define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            $(".btn-dialog").data("area", ["100%", "100%"]); //添加
            Table.api.init({
                extend: {
                    index_url: 'kf/lists/index' + location.search,
                    add_url: 'kf/lists/add',
                    // edit_url: 'kf/lists/edit',
                    del_url: 'kf/lists/del',
                    multi_url: 'kf/lists/multi' ,
                    import_url: 'kf/lists/import',
                    table: 'kf_record_list',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search:false,
                exportTypes: ['excel'],
                exportOptions: {
                    fileName: 'export_' + Moment().format("YYYY-MM-DD"),
                    preventInjection: false,
                    mso: {
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text()) ? '\\@' : '';
                        },
                    },
                    ignoreColumn: [ 'operate'] //默认不导出第一列(checkbox)与操作(operate)列
                },
                columns: [
                    [
                        // {checkbox: true},
                        // {field: 'id', title: __('Id'),operate: false},
                        {field: 'record.car_num', title: __('Record.car_num'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'phone', title: __('Record.phone'), operate: 'LIKE'},
                        {field: 'type', title: __('Type'),searchList: {
                                1: __('报案'),
                                2: __('咨询'),
                                3: __('退费'),
                                4: __('销案退费'),
                                5: __('客服催办'),
                                6: __('投诉'),
                                7: __('工商投诉'),
                                8: __('公众号投诉'),
                                9: __('支付投诉'),
                                10: __('渠道催办'),
                                11: __('舆情处理'),
                            },
                            custom: {
                                1: 'primary',
                                2: 'default',
                                3: 'warning',
                                4: 'info',
                                5: 'huang',
                                6: 'success',
                                7: 'danger',
                                8: 'primary',
                                9: 'default',
                                10: 'danger',
                            },formatter: Table.api.formatter.label},
                        {field: 'user_name', title: __('客服人员'), operate: 'LIKE', table: table, class: 'autocontent', formatter: function (value, row, index) {
                                if (value) {
                                    return value;
                                } else {
                                    return '-';
                                }
                            }},
                        {field: 'fenpei.username', title: __('查勘员'),operate: 'like'},
                        {field: 'yw_class', title: __('组织'), operate: 'LIKE'},
                        {field: 'record.car_vin', title: __('车架号'), operate: 'LIKE'},
                        {field: 'record.car_engine', title: __('发动机号'), operate: 'LIKE'},
                        {field: 'record.tong_number', title: __('服务单号'), operate: 'LIKE'},
                        {field: 'date', title: __('录入时间'), operate:'like', formatter: function (value, row, index) {
                                if (value) {
                                    return value;
                                } else {
                                    return '-';
                                }
                            }},
                        {field: 'content', title: __('Content'),operate: false,formatter: function (value, row, index) {
                                if (value) {
                                    return value;
                                } else  {
                                    return '-';
                                }
                            }},

                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'add',
                                    text: __('继续录入'), // 按钮文字，语言在对应的lang文件设置
                                    title: __('继续录入'), // 提示
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: '', // 更换图标
                                    url: 'kf/record/add/ids/{rid}',
                                    extend: 'data-area=["100%","100%"]',
                                },
                                {
                                    name: 'del',
                                    text: __('删除'), // 按钮文字，语言在对应的lang文件设置
                                    title: __('删除'), // 提示
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    icon: '', // 更换图标
                                    url: 'kf/lists/del',
                                    confirm: '确认删除吗？',
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh');
                                    },
                                    visible: function (row) {
                                        //权限判断
                                        if (Config.rule.includes('363') || Config.rule.includes(363)) {
                                            //返回true时按钮显示,返回false隐藏
                                            return true;
                                        }
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'kf/lists/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'kf/lists/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'kf/lists/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },


        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
